package com.example.administrator.liveapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.MainActivity;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.bean.RegisteredYzPageBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.CheckInfoUtill;
import com.example.administrator.liveapp.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 绑定手机号activity
 */

public class BindingPhonePage extends Activity {

    private Button binding_button;
    private ImageView binding_return;
    private EditText binding_phone;
    private EditText Message_code;
    private Button verification_code;
    private String phone;
    private RegisteredYzPageBean CodeBean;//获取注册验证码试实体类
    private int value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.binding_phone_page);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        setData();
        listener();
    }

    private void setData() {
    }

    private void initView() {
        binding_button = (Button) findViewById(R.id.binding_button);
        binding_return = (ImageView) findViewById(R.id.binding_return);
        binding_phone = (EditText) findViewById(R.id.binding_phone);
        Message_code = (EditText) findViewById(R.id.Message_code);
        verification_code = (Button) findViewById(R.id.verification_code);
    }

    /**
     * 倒计时:
     */
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            verification_code.setEnabled(false);
            verification_code.setText((millisUntilFinished / 1000) + "秒");
        }

        @Override
        public void onFinish() {
            verification_code.setEnabled(true);
            verification_code.setText("获取验证码");
        }
    };

    private void listener() {
        verification_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
                phone = binding_phone.getText().toString();
                String codeUrl = AppUrl.URLS + AppUrl.RegistrationCodeURL;
                Map<String, String> map = new HashMap<>();
                map.put("User_Phone", phone);
                map.put("Type", "5");
                XUtils.Get(codeUrl, map, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(result);
                            String code = jsonObject.optString("code");
                            if (code.equals("4400")) {
                                Gson gson = new Gson();
                                CodeBean = gson.fromJson(result, RegisteredYzPageBean.class);
                                value = CodeBean.getData().getValue();
                                Toast.makeText(BindingPhonePage.this, "已发送验证码", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(BindingPhonePage.this, "发送验证码失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        LogUtil.e(ex.getMessage() + "联网请求失败");
                    }
                });
            }
        });

        binding_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!Message_code.getText().toString().equals("")&&Message_code.getText().toString()!=null){
                    if (Integer.parseInt(Message_code.getText().toString())==value) {
                        phone = binding_phone.getText().toString();
                        String url = AppUrl.URL + AppUrl.binding;
                        Map<String, String> map = new HashMap<>();
                        map.put("openid", ApplicationUtils.getInstance().loginBean.getWxopenid().toString());
                        map.put("status", "1");
                        map.put("consumerTel", phone);
                        XUtils.Get(url, map, new MyCallBack<String>() {
                            @Override
                            public void onSuccess(String result) {
                                super.onSuccess(result);
                                JSONObject jsonObject = null;
                                try {
                                    jsonObject = new JSONObject(result);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                String code = jsonObject.optString("code");
                                if (code.equals("41100")) {
                                    // 广播通知刷新
                                    Intent intent = new Intent();
                                    intent.setAction("action.refreshFriend");
                                    sendBroadcast(intent);
                                    finish();
                                    overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                                    Toast.makeText(BindingPhonePage.this, "绑定成功", Toast.LENGTH_SHORT).show();
                                } else if(code.equals("41104")){
                                    Toast.makeText(BindingPhonePage.this, "该手机号已是用户，不能被绑定", Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(BindingPhonePage.this, code+"绑定失败", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                super.onError(ex, isOnCallback);
                                Toast.makeText(BindingPhonePage.this, ex.getMessage() + "绑定错误", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onFinished() {
                                super.onFinished();
                            }
                        });
                    } else {
                        Toast.makeText(BindingPhonePage.this, "请输入正确验证码", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(BindingPhonePage.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                }

            }
        });

        binding_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
